---
layout: "default"
title: "Character"
description: "Swift documentation for 'Character': A single extended grapheme cluster, which approximates a user-perceived."
keywords: "Character,struct,swift,documentation,<,==,write,customMirror,customPlaygroundQuickLook,debugDescription,description,hashValue"
root: "/v3.0"
---

<div class="intro-declaration"><code class="language-swift">struct Character</code></div>

<div class="discussion comment">
    <p>A single extended grapheme cluster, which approximates a user-perceived
character.</p>

<p>The <code>Character</code> type represents a character made up of one or more Unicode
scalar values, grouped by a Unicode boundary algorithm. Generally, a
<code>Character</code> instance matches what the reader of a string will perceive as
a single character. The number of visible characters is generally the most
natural way to count the length of a string.</p>

<pre><code class="language-swift">let greeting = &quot;Hello! 🐥&quot;
print(&quot;Character count: \(greeting.characters.count)&quot;)
// Prints &quot;Character count: 8&quot;</code></pre>

<p>Because each character in a string can be made up of one or more Unicode
code points, the number of characters in a string may not match the length
of the Unicode code point representation or the length of the string in a
particular binary representation.</p>

<pre><code class="language-swift">print(&quot;Unicode code point count: \(greeting.unicodeScalars.count)&quot;)
// Prints &quot;Unicode code point count: 15&quot;

print(&quot;UTF-8 representation count: \(greeting.utf8.count)&quot;)
// Prints &quot;UTF-8 representation count: 18&quot;</code></pre>

<p>Every <code>Character</code> instance is composed of one or more Unicode code points
that are grouped together as an <em>extended grapheme cluster</em>. The way these
code points are grouped is defined by a canonical, localized, or otherwise
tailored Unicode segmentation algorithm.</p>

<p>For example, a country&#39;s Unicode flag character is made up of two regional
indicator code points that correspond to that country&#39;s ISO 3166-1 alpha-2
code. The alpha-2 code for The United States is &quot;US&quot;, so its flag
character is made up of the Unicode code points <code>&quot;\u{1F1FA}&quot;</code> (REGIONAL
INDICATOR SYMBOL LETTER U) and <code>&quot;\u{1F1F8}&quot;</code> (REGIONAL INDICATOR SYMBOL
LETTER S). When placed next to each other in a Swift string literal, these
two code points are combined into a single grapheme cluster, represented
by a <code>Character</code> instance in Swift.</p>

<pre><code class="language-swift">let usFlag: Character = &quot;\u{1F1FA}\u{1F1F8}&quot;
print(usFlag)
// Prints &quot;🇺🇸&quot;</code></pre>

<p>For more information about the Unicode terms used in this discussion, see
the <a href="http://www.unicode.org/glossary/">Unicode.org glossary</a>. In particular, this discussion
mentions <a href="http://www.unicode.org/glossary/#extended_grapheme_cluster">extended grapheme clusters</a> and <a href="http://www.unicode.org/glossary/#unicode_scalar_value">Unicode scalar
values</a>.</p>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<code class="inherits">Comparable, CustomDebugStringConvertible, CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, Equatable, ExpressibleByExtendedGraphemeClusterLiteral, ExpressibleByUnicodeScalarLiteral, Hashable, LosslessStringConvertible, TextOutputStreamable</code>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>



<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>


<h3>Initializers</h3>
<div class="declaration" id="init_-string">
<a class="toggle-link" data-toggle="collapse" href="#comment-init_-string">init(<wbr>_: String)</a><div class="comment collapse" id="comment-init_-string"><div class="p">
    <p>Creates a character from a single-character string.</p>

<p>The following example creates a new character from the uppercase version
of a string that only holds one character.</p>

<pre><code class="language-swift">let a = &quot;a&quot;
let capitalA = Character(a.uppercased())</code></pre>

<p><strong><code>s</code>:</strong>  The single-character string to convert to a <code>Character</code>
  instance. <code>s</code> must contain exactly one extended grapheme cluster.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(_ s: String)</code>

    </div></div>
</div>
<div class="declaration" id="init_-unicodescalar">
<a class="toggle-link" data-toggle="collapse" href="#comment-init_-unicodescalar">init(<wbr>_: UnicodeScalar)</a><div class="comment collapse" id="comment-init_-unicodescalar"><div class="p">
    <p>Creates a character containing the given Unicode scalar value.</p>

<p><strong><code>scalar</code>:</strong>  The Unicode scalar value to convert into a character.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(_ scalar: UnicodeScalar)</code>

    </div></div>
</div>
<div class="declaration" id="init-extendedgraphemeclusterliteral_">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-extendedgraphemeclusterliteral_">init(<wbr>extendedGraphemeClusterLiteral:)</a><div class="comment collapse" id="comment-init-extendedgraphemeclusterliteral_"><div class="p">
    <p>Creates a character with the specified value.</p>

<p>Don&#39;t call this initializer directly. It is used by the compiler when you
use a string literal to initialize a <code>Character</code> instance. For example:</p>

<pre><code class="language-swift">let oBreve: Character = &quot;o\u{306}&quot;
print(oBreve)
// Prints &quot;ŏ&quot;</code></pre>

<p>The assignment to the <code>oBreve</code> constant calls this initializer behind the
scenes.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(extendedGraphemeClusterLiteral value: Character)</code>

    </div></div>
</div>
<div class="declaration" id="init-unicodescalarliteral_">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-unicodescalarliteral_">init(<wbr>unicodeScalarLiteral:)</a><div class="comment collapse" id="comment-init-unicodescalarliteral_"><div class="p">
    <p>Creates a character with the specified value.</p>

<p>Don&#39;t call this initializer directly. It is used by the compiler when you
use a string literal to initialize a <code>Character</code> instance. For example:</p>

<pre><code class="language-swift">let snowflake: Character = &quot;❄︎&quot;
print(snowflake)
// Prints &quot;❄︎&quot;</code></pre>

<p>The assignment to the <code>snowflake</code> constant calls this initializer behind
the scenes.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(unicodeScalarLiteral value: Character)</code>

    </div></div>
</div>


<h3>Instance Variables</h3>
<div class="declaration" id="var-custommirror_-mirror">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-custommirror_-mirror">var customMirror: Mirror</a><div class="comment collapse" id="comment-var-custommirror_-mirror"><div class="p">
    <p>A mirror that reflects the <code>Character</code> instance.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var customMirror: Mirror { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-customplaygroundquicklook_-playgroundquicklook">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-customplaygroundquicklook_-playgroundquicklook">var customPlaygroundQuickLook: PlaygroundQuickLook</a><div class="comment collapse" id="comment-var-customplaygroundquicklook_-playgroundquicklook"><div class="p">
    <p>A custom playground Quick Look for this instance.</p>

<p>If this type has value semantics, the <code>PlaygroundQuickLook</code> instance
should be unaffected by subsequent mutations.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var customPlaygroundQuickLook: PlaygroundQuickLook { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-debugdescription_-string">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-debugdescription_-string">var debugDescription: String</a><div class="comment collapse" id="comment-var-debugdescription_-string"><div class="p">
    <p>A textual representation of the character, suitable for debugging.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var debugDescription: String { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-description_-string">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-description_-string">var description: String</a><div class="comment collapse" id="comment-var-description_-string"><div class="p">
    <p>A textual representation of this instance.</p>

<p>Instead of accessing this property directly, convert an instance of any
type to a string by using the <code>String(describing:)</code> initializer. For
example:</p>

<pre><code class="language-swift">struct Point: CustomStringConvertible {
    let x: Int, y: Int

    var description: String {
        return &quot;(\(x), \(y))&quot;
    }
}

let p = Point(x: 21, y: 30)
let s = String(describing: p)
print(s)
// Prints &quot;(21, 30)&quot;</code></pre>

<p>The conversion of <code>p</code> to a string in the assignment to <code>s</code> uses the
<code>Point</code> type&#39;s <code>description</code> property.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var description: String { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-hashvalue_-int">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-hashvalue_-int">var hashValue: Int</a><div class="comment collapse" id="comment-var-hashvalue_-int"><div class="p">
    <p>The character&#39;s hash value.</p>

<p>Hash values are not guaranteed to be equal across different executions of
your program. Do not save hash values to use during a future execution.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var hashValue: Int { get }</code>

    </div></div>
</div>



<h3>Instance Methods</h3>
<div class="declaration" id="func-lt_rhs_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-lt_rhs_">func &lt;(<wbr>_:<wbr>rhs:)</a>
        
<div class="comment collapse" id="comment-func-lt_rhs_"><div class="p">
    <p>Returns a Boolean value indicating whether the value of the first
argument is less than that of the second argument.</p>

<p>This function is the only requirement of the <code>Comparable</code> protocol. The
remainder of the relational operator functions are implemented by the
standard library for any type that conforms to <code>Comparable</code>.</p>

<p><strong>Parameters:</strong>
  <strong>lhs:</strong> A value to compare.
  <strong>rhs:</strong> Another value to compare.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func &lt;(lhs: Character, rhs: Character) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-eqeq_rhs_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-eqeq_rhs_">func ==(<wbr>_:<wbr>rhs:)</a>
        
<div class="comment collapse" id="comment-func-eqeq_rhs_"><div class="p">
    <p>Returns a Boolean value indicating whether two values are equal.</p>

<p>Equality is the inverse of inequality. For any values <code>a</code> and <code>b</code>,
<code>a == b</code> implies that <code>a != b</code> is <code>false</code>.</p>

<p><strong>Parameters:</strong>
  <strong>lhs:</strong> A value to compare.
  <strong>rhs:</strong> Another value to compare.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func ==(lhs: Character, rhs: Character) -&gt; Bool</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-write-to_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-write-to_">func write(<wbr>to:)</a>
        
<div class="comment collapse" id="comment-func-write-to_"><div class="p">
    <p>Writes the character into the given output stream.</p>

<p><strong><code>target</code>:</strong>  An output stream.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func write&lt;Target : TextOutputStream&gt;(to target: inout Target)</code>
    
    
</div></div>
</div>


